package com.atguigu.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/12/9 16:50
 * @注释
 */
public class RequestServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("RequestServlet doPost 到达了");

        System.out.println("-------------------通过name获取value---------------------");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("username: " + username + " password: " + password);

        System.out.println("-------------------通过name获取多个value---------------------");
        String[] hobbies = req.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println("hobby: " + hobby);
        }

        System.out.println("-------------------一次性获取所有的请求参数---------------------");
        Map<String, String[]> parameterMap = req.getParameterMap();
        parameterMap.forEach((k,v)-> System.out.println(k + ", " + Arrays.toString(v)));

        System.out.println("-------------------一次性获取所有的name值---------------------");
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String name = parameterNames.nextElement();
            System.out.println("name = " + name);
        }

        resp.getWriter().println("RequestServlet doPost Success");
    }

}
